package com.report.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.operator.Contain;
import cn.zhxu.bs.operator.Equal;
import cn.zhxu.bs.util.MapUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.report.dto.request.MemberUploadReq;
import com.report.dto.response.MemberUploadRes;
import com.report.dto.response.zu.ReportItemRes;
import com.report.entity.MemberUpload;
import com.report.entity.PDFImage;
import com.report.entity.ResearchFile;
import com.report.mapper.MemberUploadMapper;
import com.report.pdf.PdfUtils;
import com.report.service.IFileService;
import com.report.service.MemberUploadService;
import com.report.service.PDFImageService;
import com.zmh.framework.exception.ResultException;
import com.zmh.framework.oss.OssConfigure;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author summer
 * @version 1.0
 * @date 2024/8/8 14:17
 */
@Service
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
public class MemberUploadServiceImpl extends ServiceImpl<MemberUploadMapper, MemberUpload> implements MemberUploadService {
    @Override
    public void saveMemberUpload(String filePath,String fileName) {
         MemberUpload memberUpload = new MemberUpload();
         memberUpload.setPath(filePath);
         memberUpload.setMemberId(Convert.convert(Integer.class, StpUtil.getLoginId()));
         memberUpload.setFileName(fileName);
         this.save(memberUpload);
     }

     private final BeanSearcher beanSearcher;

    @Override
    public Long findDatePdfNum() {
        Map<String, Object> build = MapUtils.builder().field(MemberUploadRes::getMemberId, StpUtil.getLoginId()).op(Equal.class)
                .field(MemberUploadRes::getCreateTime).sql("$1 =DATE(NOW())").build();
        Number number = beanSearcher.searchCount(MemberUploadRes.class, build);
        return number.longValue();
    }


    @Override
    public Page<MemberUploadRes> lists(MemberUploadReq memberUploadReq) {
        Page<MemberUploadRes> page = new Page<>(memberUploadReq.getPageNum(), memberUploadReq.getPageSize());
        Map<String, Object> build = MapUtils.builder().page(memberUploadReq.getPageNum() - 1, memberUploadReq.getPageSize())
                .field(MemberUploadRes::getNickName, memberUploadReq.getNickName()).op(Contain.class).build();
        SearchResult<MemberUploadRes> search = beanSearcher.search(MemberUploadRes.class, build);
        page.setTotal(search.getTotalCount().longValue());
        page.setRecords(search.getDataList());
        return page;
    }

    private final OssConfigure ossConfigure;

    private final IFileService fileService;

    private final PDFImageService pdfImageService;

    @Override
    public void wareFile(Integer id) {
        try {
            MemberUpload memberUpload = this.getById(id);
            if(ObjectUtil.isEmpty(memberUpload)){
                throw new ResultException("数据不存在");
            }
            memberUpload.setStatus(1);
            this.updateById(memberUpload);
            Integer pages = PdfUtils.getPages(memberUpload.getPath());
            String image = PdfUtils.getImage(memberUpload.getPath(), ossConfigure.getTempFile());
            String s = uploadOss(image);
            ResearchFile researchFile=new ResearchFile();
            researchFile.setImage(s);
            researchFile.setPages(pages);
            researchFile.setUrl(memberUpload.getPath());
            researchFile.setName(memberUpload.getFileName());
            fileService.save(researchFile);


        }catch (Exception e){
            throw new ResultException(e.getMessage());
        }



    }
    public String uploadOss(String image){
        String imagePath = ossConfigure.getAccessUrl()+ossConfigure.uploadFile(FileUtil.file(image));
        return imagePath;
    }
}
